﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace WeiXinSDK.Core
{
    public class DBHelper
    {
        private DbConnection connection;
        private DBFactory DBFactory = DBFactory.GetCurrentDBFactory();

        protected DBHelper()
        {
            this.connection = DBFactory.connection;
        }

        private DBHelper _db = null;
        public DBHelper CurrentDataBase
        {
            get {
                if (_db == null)
                {
                    _db = new DBHelper();
                }
                return _db;
            }
        }

        public DbCommand GetStoredProcCommond(string storedProcedure)
        {
            DbCommand dbCommand = connection.CreateCommand();
            dbCommand.CommandText = storedProcedure;
            dbCommand.CommandType = CommandType.StoredProcedure;
            return dbCommand;
        }
        public DbCommand GetSqlStringCommond(string sqlQuery)
        {
            DbCommand dbCommand = connection.CreateCommand();
            dbCommand.CommandText = sqlQuery;
            dbCommand.CommandType = CommandType.Text;
            return dbCommand;
        }

        #region 增加参数
        public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
        {
            foreach (DbParameter dbParameter in dbParameterCollection)
            {
                cmd.Parameters.Add(dbParameter);
            }
        }
        public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Size = size;
            dbParameter.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(dbParameter);
        }
        public void AddInParameter(DbCommand cmd, string parameterName, DbType dbType, object value)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Value = value;
            dbParameter.Direction = ParameterDirection.Input;
            cmd.Parameters.Add(dbParameter);
        }
        public void AddReturnParameter(DbCommand cmd, string parameterName, DbType dbType)
        {
            DbParameter dbParameter = cmd.CreateParameter();
            dbParameter.DbType = dbType;
            dbParameter.ParameterName = parameterName;
            dbParameter.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(dbParameter);
        }
        public DbParameter GetParameter(DbCommand cmd, string parameterName)
        {
            return cmd.Parameters[parameterName];
        }

        #endregion

        #region 执行

        /// <summary>
        /// 查询获得DataSet
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public DataSet ExecuteDataSet(DbCommand cmd)
        {
            DBFactory.SetTransaction(cmd);
            DbProviderFactory dbfactory = DBFactory.dbfactory;
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dbDataAdapter.Fill(ds);
            DBFactory.Dispose();
            return ds;
        }

        /// <summary>
        /// 查询获得DataTable
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public DataTable ExecuteDataTable(DbCommand cmd)
        {
            DBFactory.SetTransaction(cmd);
            DbProviderFactory dbfactory = DBFactory.dbfactory;
            DbDataAdapter dbDataAdapter = dbfactory.CreateDataAdapter();
            dbDataAdapter.SelectCommand = cmd;
            DataTable dataTable = new DataTable();
            dbDataAdapter.Fill(dataTable);
            DBFactory.Dispose();
            return dataTable;
        }

        /// <summary>
        /// 查询获得DbDataReader
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public DbDataReader ExecuteReader(DbCommand cmd)
        {
            DbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        /// <summary>
        /// 执行insert或者update
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public bool ExecuteNonQuery(DbCommand cmd)
        {
            DBFactory.SetTransaction(cmd);
            int ret = cmd.ExecuteNonQuery();
            DBFactory.Dispose();
            return ret > 0;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public object ExecuteScalar(DbCommand cmd)
        {
            DBFactory.SetTransaction(cmd);
            object ret = cmd.ExecuteScalar();
            DBFactory.Dispose();
            return ret;
        }


        /// <summary>
        /// 查询返回一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public T Execute<T>(DbCommand cmd) where T : new()
        {
            DataTable dt = ExecuteDataTable(cmd);
            return Utility.ToInstance<T>(dt);
        }

        /// <summary>
        /// 查询返回一个实体列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public List<T> ExecuteToList<T>(DbCommand cmd) where T : new()
        {
            DataTable dt = ExecuteDataTable(cmd);
            return Utility.ToList<T>(dt);
        }
        #endregion
    }
}
